package com.baizhi.service.impl;

import com.baizhi.dao.FavoriteDao;
import com.baizhi.entity.Favorite;
import com.baizhi.service.FavoriteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 收藏(Favorite)表服务实现类
 *
 * @author makejava
 * @since 2021-09-26 23:42:37
 */
@Transactional
@Service("favoriteService")
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao, Favorite> implements FavoriteService {

    @Autowired
    private FavoriteDao favoriteDao;

    @Override
    public Boolean queryByUserIdAndVideo(Integer uid, Integer videoId) {
        LambdaQueryWrapper<Favorite> queryWrapper = new LambdaQueryWrapper<>();
        //当前用户id
        queryWrapper.eq(Favorite::getUid, uid);
        queryWrapper.eq(Favorite::getVideoId, videoId);
        Favorite favorite = favoriteDao.selectOne(queryWrapper);
        //是否收藏
        Boolean b = false;
        if (favorite != null) {
            b = true;
        }
        return b;
    }

    @Override
    public void insertFavoriter(Integer UserId, Integer videoId) {
        //先去数据库中找如果没有就添加如果有就修改
        LambdaQueryWrapper<Favorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorite::getUid, UserId);
        queryWrapper.eq(Favorite::getVideoId, videoId);
        List<Favorite> favorites = favoriteDao.selectList(queryWrapper);
        int size = favorites.size();
        System.out.println("size = " + size);
        if (size == 0) {
            Favorite entity = new Favorite();
            entity.setUid(UserId);
            entity.setVideoId(videoId);
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            int insert = favoriteDao.insert(entity);
            System.out.println("进入添加");
        } else {
            for (Favorite favorite : favorites) {
                Integer id = favorite.getId();
                LambdaQueryWrapper<Favorite> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Favorite::getId, id);
                favoriteDao.delete(queryWrapper1);
            }
            //再添加
            Favorite entity = new Favorite();
            entity.setUid(UserId);
            entity.setVideoId(videoId);
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            int insert = favoriteDao.insert(entity);

        }
    }

    @Override
    public void deleteFavoriter(Integer UserId, Integer videoId) {
        LambdaQueryWrapper<Favorite> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Favorite::getUid, UserId);
        queryWrapper.eq(Favorite::getVideoId, videoId);
        Favorite entity = new Favorite();
        entity.setDeletedAt(new Date());
        favoriteDao.update(entity, queryWrapper);
    }
}

